﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VBAccelerator.Components.Shell;
using System.IO;

namespace RoketPack.Management.Objects
{
    class Link : Object
    {
        private string p_Local = null;
        private string p_Destination = null;

        internal Link(string local, string destination)
        {
            this.p_Local = local;
            this.p_Destination = destination;

            if (!this.p_Local.ToLowerInvariant().EndsWith(".lnk"))
                this.p_Local = this.p_Local + ".lnk";
        }

        internal string Local
        {
            get
            {
                return this.p_Local;
            }
        }

        internal string Destination
        {
            get
            {
                return this.p_Destination;
            }
        }

        internal override bool Download(Installation installation, Manager.VoidLambdaDouble progressevent)
        {
            return false;
        }

        internal override bool DownloadRequired(Installation installation)
        {
            return false;
        }

        internal override bool Install(Installation installation)
        {
            try
            {
                if (System.IO.File.Exists(PathTranslator.Parse(this.p_Local, this.p_Variables)))
                    System.IO.File.Delete(PathTranslator.Parse(this.p_Local, this.p_Variables));

                ShellLink sl = new ShellLink();
                sl.Target = PathTranslator.Parse(this.p_Destination, this.p_Variables);
                sl.WorkingDirectory = System.IO.Path.GetDirectoryName(PathTranslator.Parse(this.p_Destination, this.p_Variables));
                sl.DisplayMode = ShellLink.LinkDisplayMode.edmNormal;
                sl.Save(PathTranslator.Parse(this.p_Local, this.p_Variables));

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        internal override bool Update(Installation installation)
        {
            // Handle deletion.
            if (this.DeleteOnNextUpdate)
            {
                return this.Uninstall(installation);
            }

            try
            {
                if (System.IO.File.Exists(PathTranslator.Parse(this.p_Local, this.p_Variables)))
                    System.IO.File.Delete(PathTranslator.Parse(this.p_Local, this.p_Variables));

                ShellLink sl = new ShellLink();
                sl.Target = PathTranslator.Parse(this.p_Destination, this.p_Variables);
                sl.WorkingDirectory = System.IO.Path.GetDirectoryName(PathTranslator.Parse(this.p_Destination, this.p_Variables));
                sl.DisplayMode = ShellLink.LinkDisplayMode.edmNormal;
                sl.Save(PathTranslator.Parse(this.p_Local, this.p_Variables));

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        internal override bool Uninstall(Installation installation)
        {
            try
            {
                System.IO.File.Delete(PathTranslator.Parse(this.p_Local, this.p_Variables));
                return true;
            }
            catch (FileNotFoundException)
            {
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
